//
//  SCRoundProgressView.m
//  SocialCalendar
//
//  Created by CYH on 15/12/14.
//  Copyright © 2015年 Lianxi.com. All rights reserved.
//

#import "SCRoundProgressView.h"

@interface SCRoundProgressView ()

@property (strong, nonatomic) CAShapeLayer *shapeLayer;

@end

@implementation SCRoundProgressView

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        [self initProcessBar];
    }
    return self;
}

- (void)initProcessBar {
    self.wrapperColor = [UIColor grayColor];
    self.progressBarColor = [UIColor greenColor];
    self.progressBarShadowOpacity = .1f;
    self.progressBarWidth = 2.0f;
    self.wrapperWidth = 2.0f;
    self.currentProgress = 0.0f;
}

- (void)drawRect:(CGRect)rect {
    CGRect newRect = ({
        CGRect insetRect = CGRectInset(rect, self.wrapperWidth + 0.5f, self.wrapperWidth + 0.5f);
        CGRect newRect = insetRect;
        newRect.size.width = MIN(CGRectGetMaxX(insetRect), CGRectGetMaxY(insetRect));
        newRect.size.height = newRect.size.width;
        newRect.origin.x = insetRect.origin.x + (CGRectGetWidth(insetRect) - CGRectGetWidth(newRect)) / 2;
        newRect.origin.y = insetRect.origin.y + (CGRectGetHeight(insetRect) - CGRectGetHeight(newRect)) / 2;
        newRect;
    });
    UIBezierPath *outerCircle = [UIBezierPath bezierPathWithOvalInRect:newRect];
    [self.wrapperColor setStroke];
    outerCircle.lineWidth = self.wrapperWidth;
    [outerCircle stroke];
    
}

- (CGPathRef)progressPath {
    CGFloat offset = -M_PI_2;
    CGFloat endAngle = self.currentProgress * 2 * M_PI + offset;
    CGRect rect = self.bounds;
    CGPoint center = CGPointMake(CGRectGetMidX(rect), CGRectGetMidY(rect));
    CGFloat radius = MIN(center.x, center.y) - self.progressBarWidth / 2;
    UIBezierPath *arcPath = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:offset endAngle:endAngle clockwise:1];
    return arcPath.CGPath;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    [self refreshShapeLayer];
    
    if (self.centralView) {
        self.centralView.center = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds));        
    }
}

- (CAShapeLayer *)shapeLayer {
    if (!_shapeLayer) {
        _shapeLayer = [CAShapeLayer layer];
        _shapeLayer.lineWidth = self.progressBarWidth;
        _shapeLayer.fillColor = nil;
        _shapeLayer.lineJoin = kCALineJoinBevel;
        [self.layer addSublayer:_shapeLayer];
    }
    _shapeLayer.strokeColor = self.progressBarColor.CGColor;
    _shapeLayer.shadowOpacity = self.progressBarShadowOpacity;
    return _shapeLayer;
}

- (void)refreshShapeLayer {
    self.shapeLayer.path = [self progressPath];
}

- (void)setCurrentProgress:(CGFloat)currentProgress
{
    _currentProgress = MAX(MIN(currentProgress, 1.0f), 0.0f);
    [self setNeedsLayout];
}

- (void)setCentralView:(UIView *)centralView {
    if (_centralView != centralView) {
        [_centralView removeFromSuperview];
        _centralView = centralView;
        [self addSubview:self.centralView];
    }
}

@end
